package com.wangsd.common.component;

import com.alibaba.fastjson.JSON;
import com.wangsd.common.utils.DateUtils;
import com.wangsd.web.sys.entity.SysCompany;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 代金券接口
 */
@Slf4j
public class WxApiV3CouponService extends WxApiV3Service {

    public WxApiV3CouponService(SysCompany company) {
        super(company);
    }

    /**
     * 代金券-查询批次详情API
     *
     * @param wxMchId
     * @param stockId
     * @return
     */
    public String selectStocks(String wxMchId, String stockId) {
        String url = "https://api.mch.weixin.qq.com/v3/marketing/favor/stocks/" + stockId;
        Map map = new HashMap<String, String>();
        map.put("stock_creator_mchid", wxMchId);

        String result = this.get(url, map);
        log.debug("代金券-查询批次详情API==" + result);
        return result;
    }

    /**
     * 代金券-设置消息通知地址API
     *
     * @param wxMchId
     * @param notifyUrl
     * @param isSwitch
     * @return
     */
    public String setCallback(String wxMchId, String notifyUrl, Boolean isSwitch) {
        String url = "https://api.mch.weixin.qq.com/v3/marketing/favor/callbacks";
        Map map = new HashMap<String, String>();
        map.put("mchid", wxMchId);
        map.put("notify_url", notifyUrl);
        map.put("switch", isSwitch);

        String result = this.post(url, map);
        log.debug("代金券-设置消息通知地址API==" + result);
        return result;
    }

    /**
     * 代金券-发放代金券API
     *
     * @param wxAppid
     * @param wxMchId
     * @param stockId
     * @param openid
     * @return
     */
    public String sendCoupons(String wxAppid, String wxMchId, String stockId, String openid, String outTradeNo) {
        String url = "https://api.mch.weixin.qq.com/v3/marketing/favor/users/" + openid + "/coupons";
        Map map = new HashMap<String, String>();
        map.put("stock_id", stockId);
        map.put("out_request_no", outTradeNo);
        map.put("appid", wxAppid);
        map.put("stock_creator_mchid", wxMchId);

        String result = this.post(url, map);
        log.debug("代金券-发放代金券API==" + result);
        return result;
    }

    /**
     * 商家券-创建商家券
     *
     * @param map
     * @return
     */
    public String createMerCoupon(Map map) {
        String url = "https://api.mch.weixin.qq.com/v3/marketing/busifavor/stocks";
        log.debug(JSON.toJSONString(map));
        String result = this.post(url, map);
        log.debug("商家券-创建商家券API==" + result);
        return result;
    }

    /**
     * 商家券-核销商家券
     * @param coupon_code
     * @param appid
     * @return
     */
    public String useMerCoupon(String coupon_code, String appid, String transactionId) {
        String url = "https://api.mch.weixin.qq.com/v3/marketing/busifavor/coupons/use";
        Map map = new HashMap<String, String>();
        map.put("coupon_code", coupon_code);
        map.put("appid", appid);
        if (StringUtils.isNotBlank(transactionId)) {
            map.put("use_request_no", transactionId);
        }else {
            map.put("use_request_no", System.currentTimeMillis() + "");
        }
        map.put("use_time", DateUtils.formatDate(new Date(), "yyyy-MM-dd'T'HH:mm:ss.SSS") + "+08:00");
        String result = this.post(url, map);
        log.debug("商家券-核销商家券==" + result);
        return result;
    }

    /**
     * 商家券-查询商家券事件通知地址
     *
     * @return
     */
    public String getCallBack() {
        String url = "https://api.mch.weixin.qq.com/v3/marketing/busifavor/callbacks";

        String result = this.get(url, new HashMap<>());
        log.debug("商家券-查询商家券事件通知地址==" + result);
        return result;
    }

    /**
     * 商家券-设置商家券事件通知地址
     *
     * @param notifyUrl
     * @return
     */
    public String setCallBack(String notifyUrl) {
        String url = "https://api.mch.weixin.qq.com/v3/marketing/busifavor/callbacks";
        Map map = new HashMap();
        map.put("notify_url", notifyUrl);
        String result = this.post(url, map);
        log.debug("商家券-设置商家券事件通知地址==" + result);
        return result;
    }
}
